package com.moji.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moji.common.entity.MyPage;
import com.moji.common.utils.PageUtil;
import com.moji.entity.MjAboutUs;
import com.moji.entity.MjSupportHelp;
import com.moji.entity.dto.QueryAboutUsDto;
import com.moji.entity.dto.QuerySupportHelpDto;
import com.moji.mapper.MjSupportHelpMapper;
import com.moji.service.MjSupportHelpService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.util.IdGenerator;
import com.moji.util.MoJiCommonUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 支持与帮助表 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2024-07-20
 */
@Service
public class MjSupportHelpServiceImpl extends ServiceImpl<MjSupportHelpMapper, MjSupportHelp> implements MjSupportHelpService {


    @Resource
    private IdGenerator idGenerator;

    @Override
    public MyPage<MjSupportHelp> queryByPage(QuerySupportHelpDto dto) {
        LambdaQueryWrapper<MjSupportHelp> wrapper = new LambdaQueryWrapper<>();
        //类型
        wrapper.eq(ObjectUtil.isNotEmpty(dto.getType()),MjSupportHelp::getType
                ,dto.getType());
        //问题名称
        wrapper.like(ObjectUtil.isNotEmpty(dto.getQuestionName()),MjSupportHelp::getQuestionName
                ,dto.getQuestionName());
        //文档名称
        wrapper.like(ObjectUtil.isNotEmpty(dto.getDocumentName()),MjSupportHelp::getDocumentName
                ,dto.getDocumentName());
        wrapper.like(MjSupportHelp::getPrOnoff
                ,"1");
        //创建时间倒叙
        wrapper.orderByAsc(MjSupportHelp::getSort);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<MjSupportHelp> mjAboutUsList = list(wrapper);
        mjAboutUsList.forEach(item->{
            //技术支持类型 0-数据API技术相关 1-API接口文档相关 2-售后及商务问题模板"
            if("0".equals(item.getType())){
                item.setTypeName("数据API技术相关");
            }
            if("1".equals(item.getType())){
                item.setTypeName("API接口文档相关");
            }
            if("2".equals(item.getType())){
                item.setTypeName("售后及商务问题模板");
            }
        });
        PageInfo<MjSupportHelp> info = new PageInfo<>(mjAboutUsList);
        return PageUtil.PageInfo2MyPage(info);
    }

    @Override
    public boolean delete(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        return this.removeById(id);
    }

    @Override
    public boolean add(MjSupportHelp mjSupportHelp) {
        //编码自动生成
        String code = idGenerator.idGenerator("XSJS");
        mjSupportHelp.setCode(code);
        mjSupportHelp.setCreateId(MoJiCommonUtils.getUserCode());
        mjSupportHelp.setUpdateId(MoJiCommonUtils.getUserCode());
        mjSupportHelp.setCreateTime(new Date());
        mjSupportHelp.setUpdateTime(new Date());
        return this.save(mjSupportHelp);
    }

    @Override
    public boolean edit(MjSupportHelp mjSupportHelp) {
        MjSupportHelp mjSupportHelpOld=this.getById(mjSupportHelp.getId());
        if(mjSupportHelpOld ==null){
            throw new RuntimeException("数据不存在,请核查!");
        }
        BeanUtils.copyProperties(mjSupportHelp,mjSupportHelpOld);
        mjSupportHelpOld.setUpdateId(MoJiCommonUtils.getUserCode());
        mjSupportHelpOld.setUpdateTime(new Date());
        return this.updateById(mjSupportHelpOld);
    }

    @Override
    public MjSupportHelp detail(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        return this.getById(id);
    }

    @Override
    public boolean prOnOff(Long id, String prOnOff) {
        MjSupportHelp mjSupportHelpOld=this.getById(id);
        if(mjSupportHelpOld ==null){
            throw new RuntimeException("数据不存在,请核查!");
        }
        mjSupportHelpOld.setPrOnoff(prOnOff);
        return this.updateById(mjSupportHelpOld);
    }

    @Override
    public Map<String,String> down(Long id) {
        MjSupportHelp mjSupportHelp=this.getById(id);
        if(mjSupportHelp ==null){
            throw new RuntimeException("数据不存在,请核查!");
        }
        String count=mjSupportHelp.getDownloadCount();
        mjSupportHelp.setDownloadCount(ObjectUtil.isEmpty(count)?1+"":Integer.parseInt(count)+1+"");
        this.updateById(mjSupportHelp);
        Map<String,String> map=new HashMap<>();
        map.put("count",mjSupportHelp.getDownloadCount()+"");
        map.put("url",mjSupportHelp.getDocumentUrl());
        return map;
    }
}
